package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.bo.wx.AddressDeleteBO;
import com.cskaoyan.bean.bo.wx.WxAddressSaveBO;
import com.cskaoyan.bean.pojo.Region;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.wx.WxAdressDetailVO;
import com.cskaoyan.bean.vo.wx.WxAdressVO;
import com.cskaoyan.bean.bo.wx.SaveListBO;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.AddressList;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.wx.AddressListVO;
import com.cskaoyan.bean.vo.wx.CartIndexVo;
import com.cskaoyan.bean.vo.wx.mygroupon.AddressDetailVO;
import com.cskaoyan.service.AddressImpl;
import com.cskaoyan.service.AddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.security.Security;
import java.util.List;

// /**
//  * @author: hdf
//  * @class: AdressController
//  */
// @RestController
// @RequestMapping("wx")
// public class AddressController {

    // @Autowired
    // AddressService addressService;
    //
    //
    // @GetMapping("address/list")
    // public BaseRespVo addressList(){
    //     List<WxAdressVO> wxAdressVOList = addressService.addressList();
    //     return BaseRespVo.ok(wxAdressVOList);
    // }
    //
    // @GetMapping("address/detail")
    // public BaseRespVo addressDetail(Integer id){
    //     WxAdressDetailVO wxAdressDetailVO = addressService.addressDetail(id);
    //     return BaseRespVo.ok(wxAdressDetailVO);
    //
    // }
    //
    // @PostMapping("address/save")
    // public BaseRespVo addressSave(@RequestBody WxAddressSaveBO wxAddressSaveBO){
    //     //通过shiro获得当前用户 userId
    //
    //     Subject subject = SecurityUtils.getSubject();
    //     User principal = (User) subject.getPrincipal();
    //     Integer userId = principal.getId();
    //     int i = addressService.addressSave(wxAddressSaveBO,userId);
    //     return BaseRespVo.ok(i);
    // }
    //
    // @PostMapping("address/delete")
    // public BaseRespVo addressDelete(@RequestBody AddressDeleteBO addressDeleteBO){
    //     addressService.addressDelete(addressDeleteBO);
    //     return BaseRespVo.ok();

    // }


    //
    //
    // @GetMapping("region/list")
    // public BaseRespVo regionList(Integer pid){
    //     List<Region> regions = addressService.regionList(pid);
    //     return BaseRespVo.ok(regions);
    // }




/**
 * 收货地址接口
 *
 * @author: ckd
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("wx/address")
public class AddressController {

    @Autowired
    AddressService addressService;
    @RequestMapping("list")
    public BaseRespVo addressList() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        AddressListVO addressListVOS=addressService.addressList(user.getId());
        return BaseRespVo.ok(addressListVOS.getAddressLists(),"成功");
    }

    @RequestMapping("detail")
    public BaseRespVo addressList(int id) {
        AddressDetailVO addressDetailVO=addressService.addressDetail(id);
        return BaseRespVo.ok(addressDetailVO,"成功");
    }

    @RequestMapping("delete")
    public BaseRespVo deleteList(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        addressService.deleteList(id);
        return BaseRespVo.ok();
    }
    @RequestMapping("save")
    public BaseRespVo saveList(@RequestBody SaveListBO saveListBO) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail("请登录");
        }
        addressService.SaveAddress(saveListBO,user);
        return BaseRespVo.ok(saveListBO.getId(),"成功");
    }
}
